//
//  ViewController.m
//  M了个 J
//
//  Created by dllo on 16/1/19.
//  Copyright © 2016年 Scott. All rights reserved.
//

#import "ViewController.h"
#import <MJRefreshHeader.h>
#import <MJRefreshNormalHeader.h>

@interface ViewController () <UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) UIImage *image;
@property (nonatomic, retain) NSData *data;

@end

@implementation ViewController

- (void)viewDidLoad {
   self.data = [NSData data];
    [super viewDidLoad];
   
    [self createTableView];
    
}

- (void)createTableView {
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        NSURL *url = [NSURL URLWithString:@"http://www.qqpk.cn/Article/UploadFiles/201111/2011112212074796.jpg"];

        NSURLSession *session = [NSURLSession sharedSession];

      NSURLSessionDataTask *task = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {

          dispatch_async(dispatch_get_main_queue(), ^{


              self.data = data;

              NSLog(@"2");
              [self.tableView.mj_header endRefreshing];

              [self.tableView reloadData];

          });

      }];

        [task resume];
        
    }];
    
    
    [self.tableView.mj_header beginRefreshing];
    
    [self.view addSubview:self.tableView];
    

    self.tableView.delegate   = self;
    self.tableView.dataSource = self;
    
    
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"pool1"];
    
}



- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
    
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    

    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool1"];
    
    cell.textLabel.text = @"杨雪剑";
    cell.detailTextLabel.text = @"LOVE";
    
    NSLog(@"1");
    
    cell.imageView.image = [UIImage imageWithData:self.data];
    
    
    
    return cell;
    
}













- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
